<?php

/*
 * 分页类
 * @author php_angel <php_angel@126.com>
 * @date 2012-07-15
 */

class Page extends Base{
	
	//分页变量名称
	protected $page_name = 'p';
	
	//当前页码
	protected $current_page = 1;
	
	//记录总数
	protected $total_rows = 0;
	
	//每页显示记录数
	protected $per_page = 10;
	
	//基准路径
	protected $base_url = '/';
	
	//显示页码数
	protected $num_links = 5;
	
	//总共多少页
	protected $total_page = 0;
	
	/*
	 * 分页方式
	 * 1.在末尾增加/1
	 * 2.在末尾增加/p/1
	 * 3.传统的分页，?p=1
	 * 4.替换{p}为_1，一般用在生成静态列表时用
	 */
	protected $page_mode = 1;
	
	//上一页文字
	protected $prev_text = '上一页';
	
	//下一页文字
	protected $next_text = '下一页';
	
	//首页文字
	protected $first_text = '首页';
	
	//尾页文字
	protected $last_text = '尾页';
	
	//配置项
	protected $defaults = array(
		'page_name',
		'current_page',
		'total_rows',
		'per_page',
		'base_url',
		'num_links',
		'page_mode',
		'prev_text',
		'next_text',
		'first_text',
		'last_text'
	);

	public function __construct($config = array())
	{
		$this->config($config);
	}

	/*
	 * 初始化数据
	 * @access public
	 * @param $config array 分页参数
	 * @return void
	 */
	public function config($config = array())
	{	
		parent::config($config);
		
		//如果为?p=1模式，直接获取$_GET
		if ($this->page_mode==3)
		{
			$this->current_page = (isset($_GET[$this->page_name]) && intval($_GET[$this->page_name])) ? intval($_GET[$this->page_name]) : $this->current_page;
		}
		
		//计算总共页数
		$total_page = ceil($this->total_rows/$this->per_page);
		$this->total_page = $total_page ? $total_page : 1;
		
		//根据page_mode重新组合base_url
		switch ($this->page_mode)
		{
			case 1 :
				$this->base_url = rtrim($this->base_url, '/').'/';
				break;
			case 2 :
				$this->base_url = rtrim($this->base_url, '/').'/'.$this->page_name.'/';
				break;
			case 3 :
				$query = strstr($this->base_url, '?');
				parse_str(ltrim($query, '?'), $arr);
				if (isset($arr[$this->page_name]))
				{
					unset($arr[$this->page_name]);
				}
				$this->base_url = str_replace($query, '', $this->base_url).'?'.($arr ? (http_build_query($arr).'&'.$this->page_name.'='): ($this->page_name.'='));
				break;
			default :
				break;
		}
	}

	/*
	 * 显示分页
	 * @access public
	 * @return string
	 */
	public function show($options = 'index')
	{
		$page_str = '';
		
		if ($options && ! is_array($options))
		{
			$options = explode(',', $options);
		}
		
		if ($options)
		{
			foreach ($options as $option)
			{
				switch ($option)
				{
					case 'total' :
						$page_str .= $this->get_page_total();
						break;
					case 'index' :
						$page_str .= $this->get_page_index();
						break;
					case 'select' :
						$page_str .= $this->get_page_select();
						break;
					case 'search' :
						$page_str .= $this->get_page_search();
						break;
					default :
						$page_str .= '';
				}
			}
		}
		
		$page_str = $page_str ? $page_str : $this->get_page_index();
		
		return $page_str;
	}
	
	/*
	 * 返回总页数
	 * @access public
	 * @return integer
	 */
	public function get_total_page()
	{
		return $this->total_page;
	}

	/*
	 * 返回当前页码
	 * @access public
	 * @return integer
	 */
	public function get_current_page()
	{
		return $this->current_page;
	}
	
	/*
	 * 根据页码返回url
	 * @access public
	 * @return string
	 */
	public function url($p)
	{
		if ($this->page_mode<=3)
		{
			return $this->base_url.$p;
		}
		else
		{
			return $p==1 ? str_ireplace('{p}', '', $this->base_url) : str_ireplace('{p}', '_'.$p, $this->base_url);
		}
	}

	/*
	 * 返回页码链接
	 * @access public
	 * @return string
	 */
	public function get_page_index()
	{
		$url_index = '';
		
		$i = floor($this->num_links/2);
		
		if ($this->num_links%2==0)
		{
			$first = $this->current_page-$i+1;
			$last = $this->current_page+$i;
		}
		else
		{
			$first = $this->current_page-$i;
			$last = $this->current_page+$i;
		}

		if ($first<1)
		{
			$last = ($last+1-$first)>$this->total_page ? $this->total_page : ($last+1-$first);
			$first = 1;
		}
		elseif ($last>$this->total_page)
		{
			$first = ($first-($last-$this->total_page))<1 ? 1 : ($first-($last-$this->total_page));
			$last = $this->total_page;
		}
		
		for ($i=$first; $i<=$last; $i++)
		{
			if ($this->current_page == $i)
			{
				$url_index .= '<a href="javascript:;" class="current">'.$i.'</a>';
			}
			else
			{
				$url_index .= '<a href="'.$this->url($i).'">'.$i.'</a>';
			}
		}
		
		//首页
		$first_page = '<a href="'.($this->current_page==1 ? 'javascript:;' : $this->url(1)).'" class="first"/>'.$this->first_text.'</a>';
		
		//上一页
		$prev_page = ($this->current_page-1<1) ? ('<a href="javascript:;" class="prev">'.$this->prev_text.'</a>') : ('<a href="'.$this->url($this->current_page-1).'" class="prev">'.$this->prev_text.'</a>');
		
		//下一页
		$next_page = ($this->current_page+1>$this->total_page) ? ('<a href="javascript:;" class="next">'.$this->next_text.'</a>') : ('<a href="'.$this->url($this->current_page+1).'" class="next">'.$this->next_text.'</a>');
		
		//尾页
		$last_page = '<a href="'.($this->current_page>=$this->total_page ? 'javascript:;' : $this->url($this->total_page)).'" class="last"/>'.$this->last_text.'</a>';
		
		$url_index = $first_page.$prev_page.$url_index.$next_page.$last_page;
		return $url_index;
	}

	/*
	 * 返回select选择框
	 * @access public
	 * @return string
	 */
	public function get_page_select()
	{
		//随机的一个js函数名
		$fun = 'select_'.uniqid();

$url_select = <<<End
<script type="text/javascript">
	function {$fun}(p){
		var url = '';
		if({$this->page_mode}<=3){
			url = '{$this->base_url}'+p;
		}else{
			url = p==1 ? '{$this->base_url}'.replace('{p}','') : '{$this->base_url}'.replace('{p}','_'+p);
		}
		location.href = url;
		return false;
	}
</script>
<select onchange="{$fun}(this.value)">
End;

		//给option的页码一个区间
		$min = ($this->current_page-10)>0 ? ($this->current_page-10) : 1;
		$max = ($this->current_page+10<$this->total_page) ? ($this->current_page+10) : $this->total_page;
		
		for($i=$min;$i<=$max;$i++)
		{
			if($this->current_page == $i)
			{
				$url_select .= '<option value="'.$i.'" selected>第'.$i.'页</option>';
			}
			else
			{
				$url_select .= '<option value="'.$i.'">第'.$i.'页</option>';
			}
		}
		$url_select .= '</select>';
		return $url_select;
	}

	/*
	 * 返回文本库搜索页码
	 * @access public
	 * @return string
	 */
	public function get_page_search()
	{
		$uid = uniqid();
		//随机的js函数名
		$fun = 'go_'.$uid;
		//随机的分页属性名
		$page_id = 'p_'.$uid;
		
$url_search = <<<End
<script type="text/javascript">
	function {$fun}(){
		var p = document.getElementById('{$page_id}').value;
		if ( ! /^[1-9][0-9]*$/.test(p)){
			return false;
		}else if(p>{$this->total_page}){
			p = {$this->total_page};
		}
		var url = '';
		if({$this->page_mode}<=3){
			url = '{$this->base_url}'+p;
		}else{
			url = p==1 ? '{$this->base_url}'.replace('{p}','') : '{$this->base_url}'.replace('{p}','_'+p);
		}
		location.href = url;
		return false;
	}
</script>
<form onsubmit="return {$fun}()" style="display:inline">
	<input type="text name="{$page_id}" value="" id="{$page_id}" style="width:50px"/>
	<input type="submit" value="GO" />
</form>
End;
		return $url_search;
	}

	/*
	 * 返回记录页码信息，例如:3/10
	 * @access public
	 * @return string
	 */
	public function get_page_total()
	{
		return '<span>'.$this->current_page.'/'.$this->total_page.'</span>';
	}

	/*
	 * 设置当前页码
	 * @access public
	 * @return integer
	 */
	public function set_current_page($current_page)
	{
		$this->current_page = intval($current_page) ? intval($current_page) : $this->current_page;
	}
	
	/*
	 * 设置总记录数
	 * @access public
	 * @return integer
	 */
	public function set_total_rows($total_rows)
	{
		$this->total_rows = intval($total_rows);
	}
	
	/*
	 * 设置每页数据数
	 * @access public
	 * @return integer
	 */
	public function set_per_page($per_page)
	{
		$this->per_page = intval($per_page) ? intval($per_page) : $this->per_page;
	}
	
	/*
	 * 设置页码数
	 * @access public
	 * @return integer
	 */
	public function set_num_links($num_links)
	{
		$this->num_links = intval($num_links) ? intval($num_links) : $this->num_links;
	}
	
	/*
	 * 设置分页方式
	 * @access public
	 * @return integer
	 */
	public function set_page_mode($page_mode)
	{
		$this->page_mode = in_array(intval($page_mode), array(1, 2, 3, 4)) ? intval($page_mode) : $this->page_mode;
	}

}